/**
 * Project:   dtu.fmes.googleloon
 * File:      WindSimulationLink.java
 * License: 
 *            This file is licensed under GNU General Public License version 3
 *            http://www.gnu.org/licenses/gpl-3.0.txt
 *
 * Copyright:	Nicolai Sonne [ NicolaiSonne@hotmail.com.com ]
 * Date:      Dec 11, 2013
 */

package dtu.fmes.loon.simulation.processors;

import dtu.fmes.loon.model.Balloon;

/**
 * Adjust height of balloons according to target altitude
 * 
 * @author Nicolai Sonne
 * 
 */
public class AdjustHeightSimulationLink extends SimulationLink {

	@Override
	public void run() {
		for (Balloon balloon : css.getBalloons()) {
			if (balloon.isAlive()) {
				if (!balloon.getAltitude().equals(balloon.getTargetAltitude())) {
					// Balloon is not directly on top of target altitude
					if (balloon.getTargetAltitude() > balloon.getAltitude()) {
						// Above target altitude
						Double risingMovement = cbv.getAirPump().getRisingSpeed() * css.getSimulation().getSimulationInterval();

						// Move towards target altitude
						balloon.setAltitude(balloon.getAltitude() + risingMovement);
						if (balloon.getTargetAltitude() < balloon.getAltitude()) {
							// Set to target altitude if balloon "overshot"
							balloon.setAltitude(balloon.getTargetAltitude());
						}
					} else {
						// Below target altitude
						Double fallingMovement = cbv.getAirPump().getFallingSpeed() * css.getSimulation().getSimulationInterval();

						// Move towards target altitude
						balloon.setAltitude(balloon.getAltitude() - fallingMovement);
						if (balloon.getTargetAltitude() > balloon.getAltitude()) {
							// Set to target altitude if balloon "overshot"
							balloon.setAltitude(balloon.getTargetAltitude());
						}
					}
				}
			}
		}
	}
}
